package com.itheima.Controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class ValidatecodeControlle {
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 短信发送
     */
    @PostMapping("/validatecode/send4Order")
    public String SELL(String telephone){
        String key="telephonecode";
        String s = ValidateCodeUtils.generateValidateCode4String(5);
        log.info("发送短信验证码:{}",s);
        redisTemplate.opsForValue().set(key,s,30, TimeUnit.SECONDS);//有效期为30秒
        return s;
    }
    /**
     * 发送验证码
     */
    @GetMapping("/validatecode/send4Login")
    public Result phentolist(){
        String key="code";
        String s = ValidateCodeUtils.generateValidateCode4String(5);
        log.info("发送短信验证码:{}",s);
        redisTemplate.opsForValue().set(key,s,30, TimeUnit.SECONDS);//有效期为30秒
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS,null);
    }
}
